"""
@name : myiter
@author : wenyao
@projectname: sanchuanglianxi
"""
#打印30以内，所有能被3整除的数
# li = []
# for i in range(30):
#     if i%3 == 0:
#         li.append(i)
# print(li)
#
# #列表推导式
# li = [i for i in range(30) if i%3 == 0]
# print(li)
#
# #30以内能被3整除的数的平方
# lis = [i*i for i in range(30) if i%3 == 0]

#列表推导式，找到下面名字中含有两个e的所有名字
# names = [['Tom', 'Billy', 'Jefferson', 'Andrew', 'Wesley', 'Steven', 'Joe'],
#  ['Alice', 'Jill', 'Ana', 'Wendy', 'Jennifer', 'Sherry', 'Eva', 'Elven']]
# li = [ name for lst in names for name in lst if name.count('e')>=2]
# print(li)

#for --> lst --> for---name --if name.count('e')
# li = []
# for lst in names:
#     for name in lst:
#         if name.count('e')>=2:
#             li.append(name)
# print(li)

#三元运算与列表推导式
# print([i for i in range(100) if i % 3 == 0 ])
# print([i//2 if i%2==0 else i for i in range(100) if i % 3 == 0 ])
#
# #执行过程 for  --》 后if  -- 》前if
# li = []
# for i in range(100):
#     if i%3 == 0:
#         if i%2 == 0 :
#             li.append(i//2)
#         else:
#             li.append(i)

#字典推导式
# dic = {'a':1, 'b':2, 'c':3, 'ab':4}
# li = { k:v for k,v in dic.items() if 'a' not in k }
# print(li, type(li))

# tmp_dic = {}
# for k,v in dic.items():
#     if 'a' not in k:
#         del dic['a']
#         tmp_dic[k] = v
# # dic.pop('a')
# print(dic)
# print(tmp_dic)

##for 循环期间改变容器大小
# tmp_list = [1,2,3,4]
# for i in tmp_list:
#     if i == 2 :
#         #tmp_list.pop(tmp_list.index(2))
#         tmp_list.remove(2)
# print(tmp_list)

#将key转化小写,合并大小写的key值， {"a":6,"b":5,"c":4}
# dic = {"A":1,"B":2,"c":4,"a":5, "b":3}
# #tmp_dic = { k.lower():v for k,v in dic.items() }
# #tmp_dic = {k.lower():dic[k.lower()]+dic[k.upper()] if k.upper() in k else dic[k.lower()]  for k,v in dic.items()}
# #tmp_dic = li={k.lower():dic.get(k.upper())+dic.get(k.lower()) if k.lower() in dic.keys() and k.upper() in dic.keys() else dic.get(k) for k,v in dic.items()  }
# tmp_dic = {k.lower():dic.get(k.lower(),0)+dic.get(k.upper(),0) for k in dic}
# print(tmp_dic)

#集合推导式
# li = [-1,2,1,2,4]
# set_li = { i for i in li}
# set_li2 = {i*i for i in li}
# set_li3 = { print(i) for i in li}
# print(set_li)
# print(set_li2)

#过滤掉长度小于3的字符串，并转化成大写
# q1 = ['a','ab','abc','abcd','abcde']
# q2 = [i.upper() for i in q1 if len(i)>=3]
# print(q2)
#
# # x在0-5之间的偶数  y在0-5之间的奇数  组合
# q3 = [(x,y) for x in range(6) if x%2 == 0 for y in range(6) if y%2 == 1]
# list = [(x,y) for x in range(0,6) for y in range(0,6) if x%2 == 0 and y%2 == 1 ]
# print(q3)
# print(list)
# #对调字典的key value
# q4 = {10:'a',34:'b'}
# q5 = {m:n for n,m in q4.items()}
# print(q5)

# 可迭代对象  判断
#可迭代对象实现了__iter__方法，该方法返回一个迭代器对象。

# >>> from collections import Iterable
# >>> a = "abc"
# >>> b = [1,2,3]
# >>> c = (4,5,6)
# >>> d = {'name':'tom', 'age':22}
# >>> if isinstance(a, Iterable):
# ...    print('a is iterable')
# ...

#迭代器
#可以被“next”函数调用并不断返回下一个值的对象--迭代器
#实现了__iter__和__next__ 这两个方法的 迭代器
#如果容器中没有更多的元素了，就会抛出一个StopIteration
#__iter__ 返回迭代器本身  __next__返回下一个值
#所有的iterable 都可以通过iter()函数转变成迭代器

# str = "abc"
# str_it = iter(str)
# print(str(str_it))

#生成器实现斐波那契数列
# from itertools import islice
# def fib():
#     prev, curr = 0,1
#     while True:
#         yield curr
#         print("curr is :",curr)
#         print("*********end*********")
#         prev, curr = curr, curr+prev
#
# f = fib()
# print(next(f))
# print(next(f))
#print(list(islice(f,0,3)))


# def counter(start_at = 0):
#     count = start_at
#     while True:
#         #yield count
#         val = (yield count)
#         if val is not None:
#            count = val
#         else:
#            count += 1
# count = counter(5)
# print(next(count))
# print(count.send(100))
# print(next(count))
#count.close()
#print(next(count))

#可迭代协议
#for i in range(3):  迭代或者遍历
#生成数据
#[1,2,3] 对于一个大列表  1亿条数据  生成器去实现某种特定规则的数据输出 每次只有用到的时候才会返回数据
#生成器表达式  生成器函数  --》生成器
#next() 可迭代的迭代器 __iter__  __next__
#可迭代对象 __iter__  返回一个迭代器
#yield

#1、python2  与 python3的区别
#  输入 输出
#input  raw_input
#异常处理
#bool
#模块导入
#字符编码
#range  xrang   ---》 range
#for i in range()